package com.abcdefg.demo.work;

import com.abcdefg.demo.constants.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class WorkProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.Host);//ip 默认值localhost
        factory.setPort(Constants.Port); //默认值5672
        factory.setVirtualHost(Constants.VirtualHost);//虚拟机名称,
        factory.setUsername(Constants.UserName);//⽤⼾名
        factory.setPassword(Constants.Password);//密码
        //3. 创建连接Connection
        Connection connection = factory.newConnection();
        //4. 创建channel通道
        Channel channel = connection.createChannel();
        //5.声明一个队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        //6. 通过channel发送消息到队列中
        for (int i = 0; i < 10; i++) {
            String msg="hello world..."+i;
            channel.basicPublish("",Constants.WORK_QUEUE,null,msg.getBytes());
        }
        //7.释放资源
        channel.close();
        connection.close();
    }

}
